#include "widget.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    serialDataTotalTxCnt = 0;
    ui->setupUi(this);
    /* 遍历可用串口设备 */
    QStringList SerialPort_Name;

    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        qDebug()<<"Name:"<<info.portName();
        qDebug()<<"Description:"<<info.description();
        qDebug()<<"Manufacturer:"<<info.manufacturer();
        SerialPort_Name << info.portName()+':'+info.description();
        serialDevice << info.portName();
    }
    /* 添加到串口设备显示框 */
    ui->SerialNum_Box->addItems(SerialPort_Name);

    /* 获取最长字符串 */
    int maxlen = 0;
    for (int index = 0; index < ui->SerialNum_Box->count(); index++)
    {
        if (ui->SerialNum_Box->itemText(index).length() > maxlen)
        {
            maxlen = ui->SerialNum_Box->itemText(index).length();
        }
    }
    /*获取字体磅值转换为像素值*/
    int fontsize = ui->SerialNum_Box->font().pointSize();//获取字体的磅值
    ui->SerialNum_Box->view()->setFixedWidth(fontsize * maxlen * 0.75);//设置像素值

    QStringList baudList;//波特率
    QStringList dataBitsList;//数据位
    QStringList stopBitsList;//停止位
    QStringList parityList;//校验位
//    QStringList fluidcontrolList;//流控

    baudList<<"1200"<<"2400"<<"4800"<<"9600"<<"19200"<<"38400"<<"57600"<<"115200"<<"自定义";

    ui->Bandrate_Box->addItems(baudList);
    ui->Bandrate_Box->setCurrentIndex(7);
    /*下拉项改变信号槽：重载函数若有多种不同参数类型则使用信号槽连接时有必要指定参数类型*/
    connect(ui->Bandrate_Box, QOverload<int>::of(&QComboBox::currentIndexChanged),[=](int index){
        if (index == 8) {
            ui->Bandrate_Box->setEditable(true);
            ui->Bandrate_Box->setCurrentText(NULL);
        } else {
            ui->Bandrate_Box->setEditable(false);
        }
    });

    dataBitsList<<"5"<<"6"<<"7"<<"8";
    ui->Databit_Box->addItems(dataBitsList);
    ui->Databit_Box->setCurrentIndex(3);

    stopBitsList<<"1";
    stopBitsList<<"1.5";
    stopBitsList<<"2";
    ui->Stopbit_Box->addItems(stopBitsList);
    ui->Stopbit_Box->setCurrentIndex(0);

    parityList<<"None"<<"Odd"<<"Even";
    parityList<<"Mark";
    parityList<<"Space";

    ui->Parity_Box->addItems(parityList);
    ui->Parity_Box->setCurrentIndex(0);

    connect(ui->SerialPortSwitch_Bt,SIGNAL(clicked()),this,SLOT(on_SerialPortSwitch_Bt_clicked2()));
    connect(ui->ComFlush_BT,SIGNAL(clicked()),this,SLOT(on_ComFlush_BT_clicked2()));
    connect(ui->on_ClearRx_Bt,SIGNAL(clicked()),this,SLOT(on_ClearRx_Bt_clicked2()));
    connect(ui->on_ClearTx_Bt,SIGNAL(clicked()),this,SLOT(on_ClearTx_Bt_clicked2()));
    connect(ui->Send_Bt,SIGNAL(clicked()),this,SLOT(on_Send_Bt_clicked2()));
    connect(ui->on_SelectFile_Bt,SIGNAL(clicked()),this,SLOT(on_SelectFile_Bt_clicked2()));
    /*发送文本框信号槽*/
    connect(ui->Send_TextEdit, &QPlainTextEdit::textChanged,this, [=](){
        //获取发送框字符
        SendTextEditStr = ui->Send_TextEdit->document()->toPlainText();
        if (SendTextEditStr.isEmpty())
        {
            return;
        }
        //勾选hex发送则判断是否有非法hex字符
        if (ui->HexSend_checkBox->isChecked())
        {
            char ch;
            bool flag = false;
            uint32_t i, len;
            //去掉无用符号
            SendTextEditStr = SendTextEditStr.replace(' ',"");
            SendTextEditStr = SendTextEditStr.replace(',',"");
            SendTextEditStr = SendTextEditStr.replace('\r',"");
            SendTextEditStr = SendTextEditStr.replace('\n',"");
            SendTextEditStr = SendTextEditStr.replace('\t',"");
            SendTextEditStr = SendTextEditStr.replace("0x","");
            SendTextEditStr = SendTextEditStr.replace("0X","");
            //判断数据合法性
            for(i = 0, len = SendTextEditStr.length(); i < len; i++)
            {
                ch = SendTextEditStr.at(i).toLatin1();
                if (ch >= '0' && ch <= '9') {
                    flag = false;
                } else if (ch >= 'a' && ch <= 'f') {
                    flag = false;
                } else if (ch >= 'A' && ch <= 'F') {
                    flag = false;
                } else {
                    flag = true;
                }
            }
            if(flag) QMessageBox::warning(this,"警告","输入内容包含非法16进制字符");

        }
        //QString转QByteArray
        SendTextEditBa = SendTextEditStr.toUtf8();
    });

    /*HEX发送chexkBox信号槽*/
    connect(ui->HexSend_checkBox,&QCheckBox::stateChanged,this,[=](int state){
        if (SendTextEditStr.isEmpty())
        {
            return;
        }
        //asccii与hex转换
        if (state == Qt::Checked)
        {
            //转换成QByteArray -> 转换成16进制数，按空格分开 -> 转换为大写
            SendTextEditBa = SendTextEditBa.toHex(' ').toUpper();
            ui->Send_TextEdit->document()->setPlainText(SendTextEditBa);
        }
        else
        {
            //从QByteArray转换为QString
            SendTextEditStr = SendTextEditBa.fromHex(SendTextEditBa);
            ui->Send_TextEdit->document()->setPlainText(SendTextEditStr);
        }
    });

    /*定时发送定时器*/
    TimerSend = new QTimer(this);
    /*定时器超时信号槽*/
    connect(TimerSend, &QTimer::timeout, this, [=](){
        SerialSendData(SendTextEditBa);
    });

    //定时器状态改变信号槽
    connect(ui->TimeSend_checkBox, &QCheckBox::stateChanged, this, &Widget::on_TimeSend_checkBox_stateChanged2 );
    /*文件帧发送定时器信号槽*/
    FileSendTimer = new QTimer(this);
    connect(FileSendTimer,SIGNAL(timeout()),this,SLOT(File_TimerSend()));


}

Widget::~Widget()
{
    delete ui;
}

/*
    函   数：on_SerialPortSwitch_Bt_clicked
    描   述：开关串口按键点击槽函数
    输   入：无
    输   出：无
*/
void Widget::on_SerialPortSwitch_Bt_clicked2()
{
//    qDebug()<<"sadgf";
    if (ui->SerialPortSwitch_Bt->text() == "打开")
    {
        /* 创建串口对象 */
        serial = new QSerialPort(this);
        qint32 CustomBandrate = 0;
        /* 创建接收数据信号槽 */
        connect(serial, &QSerialPort::readyRead, this, &Widget::SerialPortReadyRead_slot);
        /* 设置串口号 */
        QString dev = serialDevice.at(ui->SerialNum_Box->currentIndex());
        serial->setPortName(dev);
        /* 设置波特率 */
        switch (ui->Bandrate_Box->currentIndex())
        {
            case 0:
                serial->setBaudRate(QSerialPort::Baud1200,QSerialPort::AllDirections);
                break;
            case 1:
                serial->setBaudRate(QSerialPort::Baud2400,QSerialPort::AllDirections);
                break;
            case 2:
                serial->setBaudRate(QSerialPort::Baud4800,QSerialPort::AllDirections);
                break;
            case 3:
                serial->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);
                break;
            case 4:
                serial->setBaudRate(QSerialPort::Baud19200,QSerialPort::AllDirections);
                break;
            case 5:
                serial->setBaudRate(QSerialPort::Baud38400,QSerialPort::AllDirections);
                break;
            case 6:
                serial->setBaudRate(QSerialPort::Baud57600,QSerialPort::AllDirections);
                break;
            case 7:
                serial->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
                break;
            case 8:
                CustomBandrate = ui->Bandrate_Box->currentText().toUInt();
                serial->setBaudRate(CustomBandrate, QSerialPort::AllDirections);
                break;
            default:
                break;

        }
        /* 设置数据位 */
        switch (ui->Databit_Box->currentIndex()) {
            case 0:
                serial->setDataBits(QSerialPort::Data5);
                break;
            case 1:
                serial->setDataBits(QSerialPort::Data6);
                break;
            case 2:
                serial->setDataBits(QSerialPort::Data7);
                break;
            case 3:
                serial->setDataBits(QSerialPort::Data8);
                break;
            default:
                break;
        }

        /* 设置停止位 */
        switch (ui->Stopbit_Box->currentIndex()) {
            case 0:
                serial->setStopBits(QSerialPort::OneStop);
                break;
            case 1:
                serial->setStopBits(QSerialPort::OneAndHalfStop);
                break;
            case 2:
                serial->setStopBits(QSerialPort::TwoStop);
                break;
            default:
                break;
        }

        /* 设置校验位 */
        switch (ui->Parity_Box->currentIndex()) {
            case 0:
                serial->setParity(QSerialPort::NoParity);
                break;
            case 1:
                serial->setParity(QSerialPort::EvenParity);
                break;
            case 2:
                serial->setParity(QSerialPort::OddParity);
                break;
            default:
                break;
        }

        /* 设置流控制 */
        serial->setFlowControl(QSerialPort::NoFlowControl);
        /* 打开串口 */
        if (serial->open(QIODevice::ReadWrite) != true)
        {
            QMessageBox::critical(this, "提示", "打开失败");
            return;
        }
        /*标记串口已打开*/
        global_struct.isSerialOpen = true;
        /*标记没有文件发送*/
        global_struct.isSendFile = false;
        /*设置文本*/
        ui->SerialPortSwitch_Bt->setText("关闭");


    }
    else
    {
        /*关闭并删除串口对象*/
        serial->close();
        serial->deleteLater();
        /*标记串口已关闭*/
        global_struct.isSerialOpen = false;
        /*设置文本*/
        ui->SerialPortSwitch_Bt->setText("打开");
    }
}



/*
    函   数：on_ComFlush_BT_clicked2
    描   述：串口设备刷新按钮槽函数
    输   入：无
    输   出：无
*/
void Widget::on_ComFlush_BT_clicked2()
{
    ui->SerialNum_Box->clear();
    FindSerialPort();
}

/*
    函   数：FindSerialPort
    描   述：查找串口并添加到QComboBox
    输   入：无
    输   出：无
*/
void Widget::FindSerialPort(void)
{
    /* 遍历可用串口并添加到显示框 */
    QStringList SerialPort_Name;
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        SerialPort_Name << info.portName()+':'+info.description();
        serialDevice << info.portName();
    }
    ui->SerialNum_Box->addItems(SerialPort_Name);
    /*设置下拉框列表宽度*/
    int maxlen = 0;
    for (int index = 0; index < ui->SerialNum_Box->count(); index++)
    {
        if (ui->SerialNum_Box->itemText(index).length() > maxlen)
        {
            maxlen = ui->SerialNum_Box->itemText(index).length();
        }
    }
    int fontsize = ui->SerialNum_Box->font().pointSize();
    ui->SerialNum_Box->view()->setFixedWidth(fontsize * maxlen * 0.75);
}



/*
    函   数：SerialPortReadyRead_slot
    描   述：readyRead()信号对应的数据接收槽函数
    输   入：无
    输   出：无
*/
void Widget::SerialPortReadyRead_slot()
{
    QString framedata;
    /*读取串口收到的数据*/
    QByteArray bytedata = serial->readAll();

    /*数据是否为空*/
    if (!bytedata.isEmpty())
    {
        if(ui->HexDisp_checkBox->isChecked())
        {
            /*hex显示*/
            framedata = bytedata.toHex(' ').trimmed().toUpper();
            ui->Receive_TextEdit->setTextColor(QColor(Qt::red));
        }
        else
        {
            /*ascii显示*/
            framedata = QString(bytedata);
            ui->Receive_TextEdit->setTextColor(QColor(Qt::magenta));
        }

        /*是否显示时间戳*/
        if (ui->TimeDisp_checkBox->isChecked())
        {
            framedata = QString("[%1]:RX -> %2").arg(QTime::currentTime().toString("HH:mm:ss:zzz")).arg(framedata);
            ui->Receive_TextEdit->append(framedata);
        }
        else
        {
            ui->Receive_TextEdit->append(framedata);
//            ui->Receive_TextEdit->insertPlainText(framedata);
        }

        /*更新接收计数*/
        dataTotalRxCnt += bytedata.length();
        ui->RxCnt_label->setText(QString::number(dataTotalRxCnt));
    }

}


/*
    函   数：on_ClearRx_Bt_clicked
    描   述：清除接收按键点击信号对应的槽函数
    输   入：无
    输   出：无
*/
void Widget::on_ClearRx_Bt_clicked2()
{
    ui->Receive_TextEdit->clear();
    ui->RxCnt_label->setText(QString::number(0));
    dataTotalRxCnt = 0;
}
/*
    函   数：on_ClearRx_Bt_clicked
    描   述：清除接收按键点击信号对应的槽函数
    输   入：无
    输   出：无
*/
void Widget::on_ClearTx_Bt_clicked2()
{
    ui->Receive_TextEdit->clear();
    ui->TxCnt_label->setText(QString::number(0));
    serialDataTotalTxCnt = 0;
}

/*
    函   数：on_Send_Bt_clicked2
    描   述：发送按键点击槽函数
    输   入：无
    输   出：无
*/
void Widget::on_Send_Bt_clicked2()
{

    if (global_struct.isSerialOpen != false)
    {

        if (global_struct.isSendFile)	//发送文件数据
        {

            if (ui->Send_Bt->text() == "发送")
            {
                ui->Send_Bt->setText("停止");

                SendFile();
            }
            else
            {
                ui->Send_Bt->setText("发送");
                FileSendTimer->stop();
            }
        }
        else	//发送发送框数据
        {
//            qDebug()<<"send data "<<endl;
            SerialSendData(SendTextEditBa);
        }
    }
    else
    {
        QMessageBox::information(this, "提示", "串口未打开");
    }
}

/*
    函   数：SendData
    描   述：串口发送数据
    输   入：无
    输   出：无
*/
void Widget::SerialSendData(QByteArray baData)
{
    if (baData.isEmpty() != true)
    {
        /*是否加回车换行*/
        if (ui->AddNewLine_Box->isChecked())
        {
            baData.append("\r\n");
        }

        if (ui->HexSend_checkBox->isChecked())  // hex发送
        {
            /*获取hex格式的数据*/
            baData = baData.fromHex(baData);
            /*发送hex数据*/
            serial->write(baData);
            /*是否显示时间戳*/
            if (ui->TimeDisp_checkBox->isChecked())
            {
                QString strdata = baData.toHex(' ').trimmed().toUpper();
                ui->Receive_TextEdit->setTextColor(QColor("blue"));
                ui->Receive_TextEdit->append(QString("[%1]TX -> ").arg(QTime::currentTime().toString("HH:mm:ss:zzz")));
                ui->Receive_TextEdit->setTextColor(QColor("black"));
                ui->Receive_TextEdit->insertPlainText(strdata);
            }
        }
        else     //ascii发送
        {
            /*发送ascii数据*/
            serial->write(baData);
//            ui->Receive_TextEdit->append(baData);
            QString strdata = QString(baData);
            /*是否显示时间戳*/
            if (ui->TimeDisp_checkBox->isChecked())
            {

                ui->Receive_TextEdit->setTextColor(QColor("red"));
                ui->Receive_TextEdit->append(QString("[%1]TX -> ").arg(QTime::currentTime().toString("HH:mm:ss:zzz")));

            }
            ui->Receive_TextEdit->setTextColor(QColor("black"));
            if(ui->TimeDisp_checkBox->isChecked())
            {
                ui->Receive_TextEdit->insertPlainText(strdata);
            }
            else
            {
                ui->Receive_TextEdit->append(strdata);
            }

        }
        //移动光标到末尾
        ui->Receive_TextEdit->moveCursor(QTextCursor::End);
        //更新发送计数
        serialDataTotalTxCnt += baData.length();
        ui->TxCnt_label->setText(QString::number(serialDataTotalTxCnt));
    }
    else
    {
        QMessageBox::warning(this, "警告", "数据为空");
    }
}


/*
    函   数：on_TimeSend_checkBox_stateChanged
    描   述：定时发送框勾选信号对应槽函数
    输   入：无
    输   出：无
*/
void Widget::on_TimeSend_checkBox_stateChanged2(int arg1)
{
    int time;

    /*判断串口是否打开*/
    if (false == global_struct.isSerialOpen)
    {
        if (ui->TimeSend_checkBox->isChecked())
        {
            QMessageBox::information(this, "提示", "串口未打开");
        }
        return;
    }
    /*判断是否有数据*/
    if (ui->Send_TextEdit->document()->isEmpty() == true)
    {
        if (ui->TimeSend_checkBox->isChecked())
        {
            QMessageBox::warning(this, "警告", "数据为空");
        }
        return;
    }
    /*判断勾选状态*/
    if (arg1 == Qt::Checked)
    {
        /*获取设定时间*/
        time = ui->TimeSend_lineEdit->text().toInt();
        if (time > 0) {
            TimerSend->start(time);
        } else {
            QMessageBox::warning(this, "警告", "时间必须大于0");
        }
        ui->TimeSend_lineEdit->setEnabled(false);
    }
    else
    {
        /*停止发送*/
        TimerSend->stop();
        ui->TimeSend_lineEdit->setEnabled(true);
    }
}


//文件打开
void Widget::on_SelectFile_Bt_clicked2()
{
    /*选择并打开文件*/
    QString curPath = QDir::currentPath();  //系统当前目录
    QString dlgTitle = "打开文件";  //对话框标题
    QString filter = "文本文件(*.txt);;二进制文件(*.bin *.dat);;所有文件(*.*)"; //文件过滤器
    QString filepath = QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);
    QFileInfo fileinfo(filepath);


    if (filepath.isEmpty())
    {
        QMessageBox::warning(this,"警告","文件为空");
        return;
    }
    //文件路径显示到发送框
    ui->Send_TextEdit->clear();
    ui->Send_TextEdit->appendPlainText(filepath);

    QFile file(filepath);
    QTextStream stream(&file);
    if (!file.exists())
    {
        QMessageBox::warning(this,"警告","文件不存在");
        return;
    }
    if (!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::warning(this,"警告","文件打开失败");
        return;
    }
    QString displayString;

//    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
//    {
//        qDebug()<<"Can't open the file!"<<endl;
//    }
//    while(!file.atEnd())
//    {
//        QByteArray line = file.readLine();
//        QString str(line);
//        qDebug()<< str;
//        displayString.append(str);
//    }
//       ui->Receive_TextEdit->clear();
//       ui->Receive_TextEdit->setPlainText(displayString);
    stream.seek(0);//将当前读取文件指针移动到文件开始
    QByteArray data = file.readAll();
    qDebug()<< data.size();
    //编码判断
    /* 设置应用程序的编码解码器 */
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codec);



    /* 判断编码 */
    QTextCodec::ConverterState state;
//    QTextCodec *codec = QTextCodec::codecForName("GBK");
     qDebug()<<"判断编码：data"<<file.size();
    FileText = codec->toUnicode(data.constData(),file.size(),&state);
    //若有无效字符则是GBK编码
    if (state.invalidChars > 0)
    {
        //转码后返回
        FileText = QTextCodec::codecForName("UTF-8")->toUnicode(data);

    }
    else
    {
        FileText =  data;
    }

    // 文件读取

        /*判断文件类型*/
        int type = 0;
        QMimeDatabase db;
        QMimeType mime = db.mimeTypeForFile(filepath);
        if (mime.name().startsWith("text/"))
        {
            type = 1;	//文本文件
        }
        else if (mime.name().startsWith("application/"))
        {
            type = 2;	//二进制文件
        }



        /*读取文件*/
        switch(type)
        {
            case 1:
            {
                //QIODevice读取普通文本
                int filelen = file.size();
                qDebug()<<"普通文本：filelen"<<filelen;
                qDebug()<<"FileText"<<FileText.size();
//                qDebug()<<"data"<<QTextCodec::codecForName("UTF-8")->toUnicode(data);

//                file.close();
                if (data.isEmpty())
                {
                    QMessageBox::information(this, "提示", "文件内容空");
                    return;
                }
                /* 判断编码 */
            }
            break;
            case 2:
            {
                int filelen = file.size();
                qDebug()<<"二进制：filelen"<<filelen;
                QVector<char> cBuf(filelen);//储存读出数据
                //使用QDataStream读取二进制文件
                QDataStream datain(&file);
                datain.readRawData(&cBuf[0],filelen);
                file.close();
                //char数组转QString
                FileText = QString::fromLocal8Bit(&cBuf[0],filelen);
            }
            break;
        }
        if(type == 1)
        {

            //显示文件大小信息
            QString info = QString("%1%2").arg("文件大小为：").arg(file.size());
//            qDebug()<< info;
            ui->Receive_TextEdit->clear();
            ui->Receive_TextEdit->append(info);
            stream.seek(0);//将当前读取文件指针移动到文件开始
            //显示文件内容
            if (ui->HexDisp_checkBox->isChecked())
            {
                ui->Receive_TextEdit->setPlainText(FileText.toUtf8().toHex(' ').toUpper());
            }
            else
            {
                while(!file.atEnd())
                {
                    QByteArray line = file.readLine();
                    QString str(line);
    //                qDebug()<< str;
                    ui->Receive_TextEdit->append(str);
                }
                file.close();
            }
        }
        else if(type == 2)
        {
            //显示文件大小信息
            QString info = QString("%1%2").arg("文件大小为：").arg(FileText.length());
            ui->Receive_TextEdit->clear();
            ui->Receive_TextEdit->append(info);
            //显示文件内容
            if (ui->HexDisp_checkBox->isChecked())
            {
                ui->Receive_TextEdit->setPlainText(FileText.toUtf8().toHex(' ').toUpper());
            }
            else
            {
                ui->Receive_TextEdit->setPlainText(FileText);
            }
        }


        //设置显示焦点在最顶部
        ui->Receive_TextEdit->moveCursor(QTextCursor::Start,QTextCursor::MoveAnchor);

        /*标记有文件发送*/
        global_struct.isSendFile = true;
        FrameCount = 0;
        ProgressBarValue = 0;



}






/*
    函   数：SendData
    描   述：定时器发送文件
    输   入：无
    输   出：无
*/
void Widget::SendFile(void)
{
    /*按设置参数发送*/
    FrameLen = ui->PackLen_lineEdit->text().toInt(); // 帧大小
    FrameGap = ui->GapTim_lineEdit->text().toInt();  // 帧间隔
    int textlen = Widget::FileText.size();           // 文件大小
    qDebug()<<"FrameLen:"<<FrameLen;
    qDebug()<<"FrameGap:"<<FrameGap;
    qDebug()<<"textlen:"<<textlen;
    if (FrameGap <= 0 || textlen <= FrameLen)
    {
        //时间间隔为0 或 帧大小≥文件大小 则直接一次发送
        serial->write(FileText.toUtf8());
        ui->Send_Bt->setText("发送");
        qDebug()<<"send small file "<<endl;
    }
    else
    {
        qDebug()<<"send big file "<<endl;
        //按设定时间和长度发送
        FrameNumber = textlen / FrameLen; // 包数量
        LastFrameLen = textlen % FrameLen; // 最后一包数据长度
        //进度条步进值
        if (FrameNumber >= 100)
        {
            ProgressBarStep =  100.0/FrameNumber;
        }
        else
        {
            ProgressBarStep = 100 / FrameNumber;
        }
        //设置定时器
        FileSendTimer->start(FrameGap);
    }
}



    /*
        函   数：File_TimerSend
        描   述：发送文件定时器槽函数
        输   入：无
        输   出：无
    */
    void Widget::File_TimerSend(void)
    {
        qDebug()<<"FrameCount:"<<FrameCount;
        qDebug()<<"FrameNumber:"<<FrameNumber;
        qDebug()<<"ProgressBarStep:"<<ProgressBarStep;
        if (FrameCount < FrameNumber)
        {
            serial->write(FileText.mid(FrameCount * FrameLen, FrameLen).toUtf8());
            FrameCount++;
            //更新进度条
            ui->progressBar->setValue(ProgressBarValue += ProgressBarStep);
        }
        else
        {
            if (LastFrameLen > 0)
            {
                serial->write(FileText.mid(FrameCount * FrameLen, LastFrameLen).toUtf8());
                ui->progressBar->setValue(100);
            }
            /*发送完毕*/
            FileSendTimer->stop();
            FrameCount = 0;
            ProgressBarValue = 0;
            FrameNumber = 0;
            LastFrameLen = 0;
            QMessageBox::information(this, "提示", "发送完成");
            ui->progressBar->setValue(0);
            ui->Send_Bt->setText("发送");
        }
    }



